<?php
/**
 * 获取用户私有权限
 * User: Administrator
 * Date: 2018\9\14 0014
 * Time: 17:15
 */

namespace app\Lib\Model\AdminAuth\GetAuthorization;


use app\Lib\InterfaceIn\AdminAuth\GetAuthorization\GetUserAuthHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\AdminAuth\MenuAuth;
use app\Lib\Model\DbModel\AdminUserPriAuth;

class GetUserAuth implements GetUserAuthHandle
{
    /** @var UserHandle  $user */
    private $user;
    private $cache;
    private $cacheName;
    private $cacheNameTow;
    private $table;
    private $menuAuthUpdateCache = 'menuAuthUpdateTime';
    /**
     * GetUserAuthHandle constructor.
     * @param UserHandle $user
     * @param bool $cache
     */
    public function __construct(UserHandle $user, $cache = true)
    {
        $this->user  = $user;
        $this->cache = $cache;
        $this->cacheName = 'userPrivateAuth_userId_'.$this->user->getUid();
        $this->cacheNameTow = 'userPrivateAuth_userId_'.$this->user->getUid().'_updateTime';
    }

    /**
     * 外部调用执行方法
     * @return array
     */
    public function run()
    {
        return $this->getUserAuthObj();
    }

    /**
     * 删除缓存
     * @return bool
     */
    public function delCache()
    {
        cache($this->cacheName,null);
        cache($this->cacheNameTow,null);
        return true;
    }

    /**
     * 获取用户权限对象
     * @return array
     */
    private function getUserAuthObj(){
        $AuthObj = [];
        $userAuthUpdateTime = cache($this->cacheNameTow);
        $menuAuthUpdateTime = cache($this->menuAuthUpdateCache);
        $cacheStatus = $this->cache;
        /*当菜单权限缓存更新时间>角色权限缓存更新时间删除缓存*/
        if($menuAuthUpdateTime>$userAuthUpdateTime){
            $this->delCache();
            $cacheStatus = false;
        }
        /*获取权限id*/
        if($cacheStatus){
            $result =  $this->getCache();
        }else{
            $result =  $this->getUserAuth();
            cache($this->cacheName,json_encode($result));
            cache($this->cacheNameTow,time());
        }
        /*生成权限对象*/
        if(is_array($result) && !empty($result)){
            foreach ($result as $v){
                $AuthObj[]= new MenuAuth($v);
            }
        }
        return $AuthObj;
    }

    /**
     * 获取用户个人权限缓存
     * @return array
     */
    private function getCache(){
        $data = cache($this->cacheName);
        if($data){
            $data = json_decode($data,true);
        }else{
            $data = $this->getUserAuth();
            cache($this->cacheName,json_encode($data));
            cache($this->cacheNameTow,time());
        }
        return $data;
    }

    /**
     * 获取用户个人权限执行
     * @return array
     */
    private function getUserAuth(){
        $model  = $this->getTable();
        $map    = [$model->_uid=>$this->user->getUid()];
        $result = $model->where($map)->column($model->_menu_id);
        return $result;
    }

    /**
     * 获取数据库
     * @return AdminUserPriAuth
     */
    private function getTable(){
        if(empty($this->table)){
            $this->table = new AdminUserPriAuth();
        }
        return $this->table;
    }

}